<?
// /ui/commandhandler/request/group|renew|restore/group/[groupname][/status/ok]

class News_Command_DownloadRemover
{
    var $Controller;
    var $Group; 
    var $Index; 
    var $Request; 
    function News_Command_DownloadRemover($controller)
	{
	    $this->Controller = $controller;
        $this->Group      = $this->Controller->Settings['group'];  
        $this->Index      = $this->Controller->Settings['index']; 
        $this->Request    = $this->Controller->Settings['request']; 
        $this->Path       = "notify/{$this->Group}.{$this->Index}.remove";
        if ($this->Request == 'fileedit')
        {
            $this->Path .= '.fileedit';
        }
        $this->URI        = $this->Controller->URI (array('status'=>'ok'));
	}
	
	function GetURI()
	{
	    $group = $this->Request=='fileedit' ? $this->Group : NULL;
	    $uri = $this->Controller->URI (array('ui'=>'downloads', 'request'=>NULL, 'group'=>$group, 'index'=>NULL));
	    return array("RequestURI"  => $this->URI,
	                 "RedirectURI" => $uri);
	}
	
	function GetStatus()
	{
	    if (!file_exists($this->Path))
        {
            return 'OK';
        } 
        return '';
	}
	
	function Signal()
	{   
        $message =  "{$this->Group}";
        
        if (strlen($this->Controller->Settings['status']) > 0)
        {
            return $this->GetStatus ();
        } 
        
        if (isset($this->Controller->Settings['rec']) > 0)
        {
            $array = $this->Controller->Settings['rec'];
            $str   = array();
            $str[] = "group={$message}";
            for ($i=0;$i<sizeof($array);$i++)
            {
                $str[] = "rec[]=" . $array[$i];
            }
            $message = implode('&', $str);
        } 
        
        
        $this->Controller->Save($this->Path, $message); 
        return $this->Controller->Dialog ($this->GetURI());
	} 
	
	function Invoke($command)
	{  
	
	    $this->Path = $command['path'];
        $bookindex = $command['arg1'];
        
        $groupname = $this->Controller->Read($command['path']);  

	    if ($command['type']=='fileedit')
	    {
	        parse_str($groupname);
	        $groupname  = $group; 
	        
	        for ($x=0;$x<sizeof($rec);$x++)
	        {
	            unlink ($rec[$x]);
	        }
	        unlink ($this->Path); 
	        return;
	    }
	    
        $dir = "data/perm/{$groupname}/$bookindex/"; 
        if (is_dir($dir))
        {
            $groupdirs = $this->Controller->OpenDir($dir); 
            $ret       = array();
            for ($x=0;$x<sizeof($groupdirs);$x++)
            {  
                unlink ($groupdirs[$x]['path']);
                if (file_exists($groupdirs[$x]['path'])) 
                    echo ("Could not delete " . $groupdirs[$x]['path'] . "\n");
                else
                    echo ("Deleted " . $groupdirs[$x]['path'] . "\n");
            }
            rmdir ($dir); 
            if (!is_dir($dir))
            {
		        unlink ($this->Path); 
                return true; 
            }
            else
            {
                echo "Could not remove {$dir}";
                return false;
            }
        }
        echo "Could not open {$dir}";	    
	     
        
	}
     
}



